package cn.zs.supply.common.utils;




import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;



public class ImageUtil2 {
	/**
	 * 将图片压缩到指定大小以内
	 * 
	 * @param srcImgData 源图片数据
	 * @param maxSize 目的图片大小
	 * @return 压缩后的图片数据
	 */
	public static byte[] compressUnderSize(byte[] srcImgData, long maxSize) {
		double scale = 0.9;
		byte[] imgData = Arrays.copyOf(srcImgData, srcImgData.length);
 
		if (imgData.length > maxSize) {
			do {
				try {
					imgData = compress(imgData, scale);
 
				} catch (IOException e) {
					throw new IllegalStateException("压缩图片过程中出错，请及时联系管理员！", e);
				}
 
			} while (imgData.length > maxSize);
		}
 
		return imgData;
	}
 
	/**
	 * 按照 宽高 比例压缩
	 * 
	 * @param imgIs 待压缩图片输入流
	 * @param scale 压缩刻度
	 * @param out 输出
	 * @return 压缩后图片数据
	 * @throws IOException 压缩图片过程中出错
	 */
	public static byte[] compress(byte[] srcImgData, double scale) throws IOException {
		BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
		int width = (int) (bi.getWidth() * scale); // 源图宽度
		int height = (int) (bi.getHeight() * scale); // 源图高度
 
		Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 
		Graphics g = tag.getGraphics();
		g.setColor(Color.RED);
		g.drawImage(image, 0, 0, null); // 绘制处理后的图
		g.dispose();
 
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		ImageIO.write(tag, "JPEG", bOut);
 
		return bOut.toByteArray();
	}

	
	public static void main(String[] args) {
		byte[] data = ImageUtil2.compressUnderSize(readInByteArray(new File("C:/Users/Administrator/Desktop/gGPE08I4upA230qIvtNn.jpg")), 200 * 340);
		
		try {
			FileUtils.writeByteArrayToFile(new File("C:/Users/Administrator/Desktop/gGPE08I4upA230qIvtNn1234.jpg"), data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param filePath 原图片路径
	 * @param newFilePath 压缩后图片路径
	 * @return 返回1 代表成功  返回0 代表失败
	 */
	public static String yasuoImage(String filePath,String newFilePath){
		byte[] data = ImageUtil2.compressUnderSize(readInByteArray(new File(filePath)), 200 * 340);
		//Assert.assertTrue(data.length < 600 * 1024);
		try {
			FileUtils.writeByteArrayToFile(new File(newFilePath), data);
			return "1";
		} catch (IOException e) {
			e.printStackTrace();
			return "0";
		}
	}

	
	private static byte[] readInByteArray(File imgFile) {
		try {
			return IOUtils.toByteArray(new FileInputStream(imgFile));
 
		} catch (IOException e) {
			throw new IllegalStateException("读取待压缩图片过程中出错，请及时联系管理员！", e);
 
		}
	}

}
